package com.rs232;

import android.app.Activity;
import android.content.Intent;
import android.jb.rs232.RS232Controller;
import android.jb.rs232.RS232Controller.Callback;
import android.jb.utils.Tools;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.ht380d4G_jar_demo.R;

public class RS232Activity extends Activity implements Callback {
	
	private Spinner baurateSp, checkSp, dataBitsSp, stopBitsSp;
	private CheckBox cb;
	private EditText et;
	private TextView tv;
	private Button btn;
	private RS232Controller rs232Con;
	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			byte[] data = (byte[]) msg.obj;
			switch (msg.what) {
			case 0:
				if (cb.isChecked())
					tv.setText(Tools.bytesToHexString(data, 0,
							data.length));
				else
					tv.setText(new String(data));
				break;

			default:
				break;
			}
		};
	};

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		sendBroadcast(new Intent("ReleaseCom"));
		try {
			Thread.sleep(200);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("RS232Activity_onResume");
		rs232Con = RS232Controller.getInstance();
		baurateSp.setSelection(0);
		initCon();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		if (rs232Con != null) {
			rs232Con.Rs232_Open(9600, 8, 'N', 1,
					RS232Activity.this , this);
			rs232Con.Rs232_Close();
		}
		sendBroadcast(new Intent("ReLoadCom"));
		System.out.println("RS232Activity_onPause");
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_rs232);
		cb = (CheckBox) findViewById(R.id.rs232_cb);
		et = (EditText) findViewById(R.id.rs232_et);
		baurateSp = (Spinner) findViewById(R.id.rs232_baurate_sp);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, getResources()
						.getStringArray(R.array.butrat));
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		baurateSp.setAdapter(adapter);
		baurateSp.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				// TODO Auto-generated method stub
				initCon();

			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});

		tv = (TextView) findViewById(R.id.rs232_show_received_tv);
		btn = (Button) findViewById(R.id.rs232_sendbtn);
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				try {
					String etStr = et.getText().toString();
					if (etStr != null && !etStr.trim().equals("")) {
						try {
//							rs232Con.converToRS232();
							if (cb.isChecked())
								rs232Con.Rs232_Write(Tools
										.hexString2Bytes(etStr));
							else
								rs232Con.Rs232_Write(etStr.getBytes());
						} catch (Exception e) {
							// TODO: handle exception
							e.printStackTrace();
							tv.setText("error:" + e.getMessage());
						}

					} else {

						Toast.makeText(
								RS232Activity.this,
								getString(R.string.rs232_cannot_send_empty_string),
								Toast.LENGTH_SHORT).show();
					}

				} catch (NumberFormatException e) {
					// TODO: handle exception
					e.printStackTrace();
				}

			}
		});
		ArrayAdapter<String> checkAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, getResources()
						.getStringArray(R.array.event));
		checkAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		checkSp = (Spinner) findViewById(R.id.rs232_check_bits_sp);
		checkSp.setAdapter(checkAdapter);
		checkSp.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				// TODO Auto-generated method stub
				initCon();
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});
		checkAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, getResources()
						.getStringArray(R.array.data_bits));
		checkAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		dataBitsSp = (Spinner) findViewById(R.id.rs232_data_bits_sp);
		dataBitsSp.setAdapter(checkAdapter);
		dataBitsSp.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				// TODO Auto-generated method stub
				initCon();
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});

		checkAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, getResources()
						.getStringArray(R.array.stop_bits));
		checkAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		stopBitsSp = (Spinner) findViewById(R.id.rs232_stop_bits_sp);
		stopBitsSp.setAdapter(checkAdapter);
		stopBitsSp.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				// TODO Auto-generated method stub
				initCon();
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});
		checkSp.setSelection(0);
		dataBitsSp.setSelection(0);
		stopBitsSp.setSelection(0);
	}

	protected void initCon() {
		// TODO Auto-generated method stub
		rs232Con = RS232Controller.getInstance();
		int baud = 1200;
		int dataBit = 8;
		int stopBit = 1;
		char even = 'N';
		try {
			baud = Integer.valueOf((String) baurateSp.getSelectedItem());
			dataBit = (Integer.valueOf((String) dataBitsSp.getSelectedItem()));
			stopBit = (Integer.valueOf((String) stopBitsSp.getSelectedItem()));

//			even = (Character.valueOf(((String) checkSp.getSelectedItem())
//					.charAt(0)));
			if(checkSp.getSelectedItemPosition() == 1){
				even = 'O';
			}else if (checkSp.getSelectedItemPosition() == 2) {
				even = 'E';
			}else {
				even = 'N';
			}
		} catch (NumberFormatException e) {
			// TODO: handle exception
			e.printStackTrace();
		}

		rs232Con.Rs232_Open(baud, dataBit, even, stopBit,
				RS232Activity.this , this);
	}

	@Override
	public void RS232_Read(byte[] data) {
		// TODO Auto-generated method stub
		if (data != null)
			mHandler.sendMessage(mHandler.obtainMessage(0, data));
	}
}
